package com.bacel.nb.product.web.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 报表日期管理器
 */
public final class DateHelper {

    private DateHelper() {

    }
    /**
     * 返回格式化日期，如2016-11-26
     * @param timestamp 时间戳
     * @return 日期字符串
     */
    public static String getFormatDay(final String timestamp) {
        String dayStr = "";
        try {
            Long stampValue = Long.parseLong(timestamp);
            Date date = new Date(stampValue);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            dayStr = dateFormat.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dayStr;
    }
    /**
     * 返回格式化日期，如20161126
     * @param timestamp 时间戳
     * @return 日期字符串
     */
    public static String getFormatDay2(final String timestamp) {
        String dayStr = "";
        try {
            Long stampValue = Long.parseLong(timestamp);
            Date date = new Date(stampValue);
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
            dayStr = dateFormat.format(date);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dayStr;
    }

    /**
     * 计数两个日期之间的天数，取闭区间（包含起始和终止）
     * @param fromTime 起始日期时间戳
     * @param toTime 终止日期时间戳
     * @return 天数
     */
    public static Integer getDayCount(final String fromTime,final String toTime) {
        Integer count = 0;
        try {
            Long fromStamp = Long.parseLong(fromTime);
            Long toStamp = Long.parseLong(toTime);
            Long betweenStamp = (toStamp - fromStamp)/(1000 * 3600 * 24);
            if (betweenStamp >= 0) {
                count = betweenStamp.intValue();
            }
            //闭区间+1天
            count = count + 1;
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return count;
    }

    /**
     * 获取从某天往前推n天的日期
     * @param fromDay 格式化的起始日期，如2016-11-29
     * @param dayCount 间隔天数
     * @return 格式化日期，如2016-11-12
     */
    public static String getPastDay(final String fromDay,final Integer dayCount) {

        String beginDay = "";
        try {
            SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            Date fromDate = formatDate.parse(fromDay);
            calendar.setTime(fromDate);
            calendar.set(Calendar.DATE,calendar.get(Calendar.DATE) - dayCount);

            Date beginTime = calendar.getTime();
            beginDay = formatDate.format(beginTime);

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return beginDay;
    }

    /**
     * 将时间戳转换成Date对象
     * @param timestamp 时间戳
     * @return Date对象
     */
    public static Date getDate(final String timestamp) {
        Date day = null;
        try {
            Long stampValue = Long.parseLong(timestamp);
            day = new Date(stampValue);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return day;
    }
}
